package com.kuang.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuang.entity.Banner;
import com.kuang.mapper.BannerMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {


    /**
     * 查询小程序的轮播图
     *
     * @param pageNo   起始页
     * @param pageSize 每页显示多少条
     * @return
     */
    public List<Banner> findBanners(int pageNo, int pageSize) {
        // 1 :设置查询分页信息
        Page<Banner> page = new Page<>(pageNo, pageSize);
        // 2 :设置查询条件
        LambdaQueryWrapper<Banner> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询发布的轮播图信息
        lambdaQueryWrapper.eq(Banner::getStatus, 1);
        // 查询进行appsort的升序返回
        lambdaQueryWrapper.orderByAsc(Banner::getAppsort);
        // 3 :查询返回
        Page<Banner> bannerPage = this.page(page, lambdaQueryWrapper);
        // 4： 分页返回轮播图信息
        List<Banner> records = bannerPage.getRecords();
        return CollectionUtils.isEmpty(records) ? new ArrayList<>() : records;
    }
}
